import ColliderTrigger, { ColliderEvents } from "./ColliderTrigger";

export default class Collision {
    displayObject: Laya.Sprite3D;
    trigger: ColliderTrigger[];
    public _model: Laya.Sprite3D;

    initDisplay(displayObject: Laya.Sprite3D) {
        this.displayObject = displayObject;
        this.displayObject.active = true;
        this._model = this.getModel() || this.displayObject;
        this.trigger = this.getTrigger();
        this.onEvent();
    }

    protected getModel() {
        return this.displayObject.getChildAt(0) as Laya.Sprite3D;
    }

    // 为需要加碰撞检测的物体添加ColliderTrigger   方便多个碰撞单位一体情况
    protected getTrigger(): ColliderTrigger[] {
        return [this._model.addComponent(ColliderTrigger)];
    }

    onEvent() {
        if (this.trigger) {
            this.trigger.forEach((trigger: ColliderTrigger) => {
                if (trigger) {
                    trigger.eventer.on(ColliderEvents.TRIGGER_START, this, this.onTriggerEnter, [trigger]);
                    trigger.eventer.on(ColliderEvents.TRIGGER_STAY, this, this.onTriggerStay, [trigger]);
                    trigger.eventer.on(ColliderEvents.TRIGGER_EXIT, this, this.onTriggerExit, [trigger]);
                    trigger.eventer.on(ColliderEvents.COLLIDE_START, this, this.onCollisionEnter, [trigger]);
                    trigger.eventer.on(ColliderEvents.COLLIDE_STAY, this, this.onCollisionStay, [trigger]);
                    trigger.eventer.on(ColliderEvents.COLLIDE_EXIT, this, this.onCollisionExit, [trigger]);
                }
            })
        }
    }

    offEvent() {
        if (this.trigger) {
            this.trigger.forEach((trigger: ColliderTrigger) => {
                if (trigger) {
                    trigger.eventer.off(ColliderEvents.TRIGGER_START, this, this.onTriggerEnter);
                    trigger.eventer.off(ColliderEvents.TRIGGER_STAY, this, this.onTriggerStay);
                    trigger.eventer.off(ColliderEvents.TRIGGER_EXIT, this, this.onTriggerExit);
                    trigger.eventer.off(ColliderEvents.COLLIDE_START, this, this.onCollisionEnter);
                    trigger.eventer.off(ColliderEvents.COLLIDE_STAY, this, this.onCollisionStay);
                    trigger.eventer.off(ColliderEvents.COLLIDE_EXIT, this, this.onCollisionExit);
                }
            })
        }
    }

    onTriggerEnter(trigger: ColliderTrigger, collision: Laya.PhysicsComponent) {
    }

    onTriggerExit(trigger: ColliderTrigger, event: Laya.PhysicsComponent) {
    }

    onTriggerStay(trigger: ColliderTrigger, event: Laya.PhysicsComponent) {
    }

    onCollisionEnter(trigger: ColliderTrigger, collision: Laya.Collision) {
    }

    onCollisionStay(trigger: ColliderTrigger, collision: Laya.Collision) {
    }

    onCollisionExit(trigger: ColliderTrigger, collision: Laya.Collision) {
    }

    dispose() {
        this.offEvent();
        if (this.trigger) {
            this.trigger.forEach((trigger: ColliderTrigger) => {
                trigger.destroy();
            })
            this.trigger = null;
        }
        if (this.displayObject) {
            this.displayObject.destroy();
            this.displayObject = null;
        }
    }
}